package com.shanghaionstar.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.map.util.ToastUtil;
import com.onstar.cn.sqlite.entities.BroadcastHeaderEntity;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.webview.handler.BroadcastDetailWebHandler;
import com.shanghaionstar.activity.webview.handler.CommonWebViewActivity;
import com.shanghaionstar.activity.webview.handler.IWebType;
import com.shanghaionstar.activity.webview.handler.WebDataEntity;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.user.adapter.BroadCastListAdapter;
import com.shanghaionstar.user.iview.IBroadcastView;
import com.shanghaionstar.user.presenter.BroadcastPresenter;
import com.shanghaionstar.utils.BusinessUtil;

import java.util.List;

/**
 * 先从网络加载消息列表，成功后存到数据库，再从数据库加载显示
 *
 * Created by 7828wc on 16/6/30.
 */
public class BroadcastFragment extends BaseFragment implements IBroadcastView,AdapterView.OnItemClickListener{

    @FindViewById
    private View pl_loading;
    @FindViewById
    private TextView tv_noDatas;
    @FindViewById
    private ListView lsv_msgs;

    private BroadCastListAdapter adapter;

    private BroadcastPresenter broadcastPresenter;

    public BroadcastPresenter getBroadcastPresenter() {
        if (broadcastPresenter == null) {
            broadcastPresenter = new BroadcastPresenter(baseActivity.getScheduler(), baseActivity);
            broadcastPresenter.setBroadcastView(this);
        }
        return broadcastPresenter;
    }

    @Override
    public int getLayoutId() {
        return R.layout.onstar_broadcastlist;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        lsv_msgs.setOnItemClickListener(this);
        init();
    }

    private void init(){
        loadMsgs();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView tv_messageid = (TextView)
                view.findViewById(R.id.tv_messageid);
        TextView broadcast_title = (TextView) view.findViewById(R.id.broadcast_title);
        String message_id = tv_messageid.getText().toString();
        WebDataEntity webDataEntity = new WebDataEntity();
        webDataEntity.setClzName(BroadcastDetailWebHandler.class.getName());
        webDataEntity.setWhat(IWebType.TYPE_BROADCAST_DETAIL);
        if (!CommonUtil.isNull(broadcast_title.getText().toString()))
            webDataEntity.setTopTitle("   　");
        webDataEntity.setCanGoBack(true);
        Bundle bundle = new Bundle();
        if (!CommonUtil.isNull(message_id))
            bundle.putString("message_id", message_id);
        CommonWebViewActivity.launchCommonWeb(getActivity(),bundle,webDataEntity);


//        if (!CommonUtil.isNull(broadcast_title.getText().toString()))
//            bundle.putString("title", broadcast_title.getText().toString());
        BroadcastHeaderEntity headerEntity = (BroadcastHeaderEntity)parent.getAdapter().getItem(position);
        headerEntity.setIsRead("true");
        adapter.notifyDataSetChanged();
        getBroadcastPresenter().update(headerEntity);
//
//        bundle.putSerializable("msg", headerEntity);
//        detail_fragment.setArguments(bundle);
//        baseActivity.changeFragment(baseActivity.getSupportFragmentManager(), detail_fragment,

        BusinessUtil.sendReportByClickType(baseActivity, FunctionNameFindId.My_Message_ClickMessage.getStringValue());


    }

    /**
     * 从网络加载消息
     */
    private void loadMsgs(){
        pl_loading.setVisibility(View.VISIBLE);
        getBroadcastPresenter().loadMsgs();
    }

    private void setAdapter(List<BroadcastHeaderEntity> msglist){
        if (adapter == null)
            adapter = new BroadCastListAdapter(msglist,baseActivity);
        lsv_msgs.setAdapter(adapter);
    }

    @Override
    public void onLoadMsgComplete() {
        getBroadcastPresenter().getAllListFromDB();
    }

    @Override
    public void showMsgList(List<BroadcastHeaderEntity> list) {
        pl_loading.setVisibility(View.GONE);
        tv_noDatas.setVisibility(View.GONE);
        lsv_msgs.setVisibility(View.VISIBLE);
        setAdapter(list);
    }

    @Override
    public void showNoMsg() {
        pl_loading.setVisibility(View.GONE);
        lsv_msgs.setVisibility(View.GONE);
        tv_noDatas.setVisibility(View.VISIBLE);
    }

    @Override
    public void onLoadFromServiceErr(Throwable throwable) {
        if (throwable != null && TextUtils.isEmpty(throwable.getMessage()))
            ToastUtil.show(baseActivity,throwable.getMessage());
    }

    @Override
    public void onLoadFromDBErr(Throwable throwable) {
        pl_loading.setVisibility(View.GONE);
        if (lsv_msgs.getAdapter() == null || lsv_msgs.getAdapter().getCount() == 0){
            lsv_msgs.setVisibility(View.GONE);
            tv_noDatas.setVisibility(View.VISIBLE);
        }else {
            tv_noDatas.setVisibility(View.GONE);
            lsv_msgs.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (broadcastPresenter != null){
            broadcastPresenter.finish();
            broadcastPresenter = null;
        }
    }
}